﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using BCCommon.OAEnums.Attendance;
using BCEntity.OA.Attendance.AttendanceRemedyApply;
using BCCommon;

namespace BCData.OA.Attendance.AttendanceRemedyApply
{
    public class AttendanceRemedyApplyData : IAttendanceRemedyApplyData
    {
        private const string QUERY = @"SELECT 
	                                    RemedyApplyId,
	                                    ApplyCode,
	                                    RemedyDate,
                                        RemedyTime,
                                        RemedyReason,
                                        RemarkFiles,
                                        ClassId,
                                        ClassName,
                                        AdminId,
                                        AdminName,
                                        CreateTime,ModifierId,
                                        ModifierName,
                                        ModifyTime,
                                        `Status`,CompanyId,
                                        DepartmentId,
                                        DepartmentName,
                                        WeekDay,
                                        AttendanceGroupId 
                                    FROM OA_AttendanceRemedyApply";

        private readonly IDatabaseContext databaseContext;

        public AttendanceRemedyApplyData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        /// <summary>
        /// 新增一个补卡申请信息
        /// </summary>
        /// <param name="entity">补卡信息</param>
        /// <returns></returns>
        public async Task<AttendanceRemedyApplyEntity> InsertAsync(AttendanceRemedyApplyEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_AttendanceRemedyApply", new string[] {"ApplyCode","RemedyDate","RemedyTime","RemedyReason", "RemarkFiles",
                                                          "CompanyId","ClassId", "ClassName","AdminId","AdminName","DepartmentId","DepartmentName","WeekDay","AttendanceGroupId"  });
            sql = string.Format("{0} {1} WHERE RemedyApplyId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?ClassId", entity.ClassId, DbType.Int64);
            parameters.Add("?ClassName", entity.ClassName, DbType.AnsiString, size: 50);
            parameters.Add("?AttendanceGroupId", entity.AttendanceGroupId, DbType.Int64);
            parameters.Add("?DepartmentName", entity.DepartmentName, DbType.AnsiString, size: 50);
            parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32);
            parameters.Add("?DepartmentId", entity.DepartmentId, DbType.Int64);
            parameters.Add("?AdminId", entity.AdminId, DbType.Int64);
            parameters.Add("?AdminName", entity.AdminName, DbType.AnsiString, size: 50);
            parameters.Add("?ApplyCode", entity.ApplyCode, DbType.AnsiString, size: 50);
            parameters.Add("?WeekDay", entity.WeekDay, DbType.AnsiString, size: 50);
            parameters.Add("?RemedyTime", entity.RemedyTime, DbType.AnsiString, size: 100);
            parameters.Add("?RemedyDate", entity.RemedyDate, DbType.Date);
            parameters.Add("?RemedyReason", entity.RemedyReason, DbType.AnsiString, size: 1000);
            parameters.Add("?RemarkFiles", entity.RemarkFiles, DbType.AnsiString, size: 100);
            
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AttendanceRemedyApplyEntity>(sql, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 获取补卡申请历史记录
        /// </summary>
        /// <param name="adminId">员工Id</param>
        /// <returns></returns>

        public async Task<IEnumerable<AttendanceRemedyApplyEntity>> GetEntitiesAsync(long? adminId)
        {
            string sql = string.Format("{0}", QUERY);
            DynamicParameters parameter = new DynamicParameters();
            if (adminId.HasValue)
            {
                sql += " WHERE AdminId=?AdminId";
                parameter.Add("?AdminId", adminId.Value, DbType.Int64);
            }

            return await this.databaseContext.Slave.QueryAsync<AttendanceRemedyApplyEntity>(sql, parameter);
        }

        /// <summary>
        /// 补卡申请详情
        /// </summary>
        /// <param name="remedyApplyId">补卡申请Id</param>
        /// <returns></returns>
        public async Task<AttendanceRemedyApplyEntity> GetByIdAsync(long remedyApplyId)
        {
            string sql = string.Format("{0} WHERE RemedyApplyId=?RemedyApplyId ", QUERY);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?RemedyApplyId", remedyApplyId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<AttendanceRemedyApplyEntity>(sql, parameter);

        }

        /// <summary>
        /// 更新指定的补卡申请单状态
        /// </summary>
        /// <param name="applyCode">申请单号</param>
        /// <param name="modifierName">修改人</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public async Task<AttendanceRemedyApplyEntity> UpdateStatus(string applyCode, string modifierName, RemedyApplyStatus status, long modifierId)
        {
            string sql = SqlCommands.GetUpdateSql("OA_AttendanceRemedyApply", new string[] { "Status", "ModifierId", "ModifierName", "ModifyTime" }, new string[] { "ApplyCode" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?Status", status.ToString(), DbType.AnsiString);
            parameters.Add("?ModifierId", modifierId, DbType.Int64);
            parameters.Add("?ModifierName", modifierName, DbType.AnsiString, size: 50);
            parameters.Add("?ModifyTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?ApplyCode", applyCode, DbType.AnsiString);
            string querySql = string.Format("{0} {1} WHERE ApplyCode=?ApplyCode", sql, QUERY);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AttendanceRemedyApplyEntity>(querySql, parameters,databaseContext.Transaction);
        }

        /// <summary>
        /// 根据申请编号，获取对应的补卡申请信息
        /// </summary>
        /// <param name="applyCode">申请编号</param>
        /// <returns></returns>
        public async Task<AttendanceRemedyApplyEntity> GetByApplyCodeAsync(string applyCode)
        {
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?ApplyCode", applyCode, DbType.AnsiString);
            string querySql = string.Format("{0} WHERE ApplyCode=?ApplyCode", QUERY);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<AttendanceRemedyApplyEntity>(querySql, parameters);
        }
    }
}
